The program will work by picking a random phrase from a collection and displaying it. This means I will need to get a random number from somewhere. There are lots of ways you can generate a random number. If you only need one value I recommend timing how long a user keeps a button down. If your processor is fast enough (and the PICmicro is) you can easily get a value this way and the user will be unable to influence the value.

However, I want a stream of random numbers, not just for the result but to allow me to fill the LCD with a magical pattern while the mystic lcd "decides" your fate. To do this I am going to use a "Pseudo Random Sequence".

Computers are not very good at producing random behaviour, but they can calculate sequences of random numbers quite easily. There are lots of ways of doing this kind of thing and many form the basis of data encryption. The numbers which are produced are a sequence because you get the next value by performing some action on the previous one. We are going to use a technique called the Linear Feedback Shift Register or LFSR which we are going to implement in software.

The LFSR works because we regard a number in the computer as a sequence of bits. We know that we can use the shift operation to move the value left or right. One part of the LFSR calculation involves shifting the value to the left and taking the bits which are about to fall off the end and feeding them back into the input.

If that was just what we did we would get back to our original value after eight shifts, so we do something cunning. We take some of the bits from the middle of the and combine them with the bit we are about to re-introduce. We use "exclusive or" to combine the bits. If we pick the right middle bits and start value we should get a sequence of numbers which is "random" in that the changes are hard to spot.

The LFSR method is easy to implement using hardware, in that you can use actual shift registers and OR gates. We are going to perform the same job using software.

In the diagram on the right I am getting "random" behaviour by combining bit 7 and bit 5 to get a new bit 0.